#ifndef _SINGLETON_H_
#define _SINGLETON_H_

#include <iostream>
using namespace std;

class A
{
public:
    static A* getInstance()
    {
        return a;
    }
private:
    A()
    {
        a = new A;
    }
    static A* a;
};

A* A::a = nullptr;

// 懒汉式，线程不安全
// 对象的创建在第一次调用 getInsstance 函数时创建
class SingletonLazy
{
public:
    static SingletonLazy* getInstance()
    {
        if (pSingleton == nullptr) {
            pSingleton = new SingletonLazy();
        }

        return pSingleton;
    }
private:
    SingletonLazy()
    {
        cout << "SingletonLazy()" << endl;
    }
    static SingletonLazy* pSingleton;
};
// SingletonLazy* SingletonLazy::pSingleton = nullptr;

// 饿汉式，线程安全
// 对象执行时优先创建
class SingletonHungry
{
public:
    static SingletonHungry* getInstance()
    {
        return pSingleton;
    }

    static void freeSpace()
    {
        if (pSingleton != nullptr) {
            delete pSingleton;
        }
    }

private:
    SingletonHungry()
    {
        cout << "SingletonHungry()" << endl;
    }
    static SingletonHungry* pSingleton;
};
// SingletonHungry* SingletonHungry::pSingleton = new SingletonHungry();

#endif // _SINGLETON_H_